package com.zzy.msm.service.impl;

import com.aliyun.auth.credentials.Credential;
import com.aliyun.auth.credentials.provider.StaticCredentialProvider;
import com.aliyun.core.http.HttpClient;
import com.aliyun.core.http.HttpMethod;
import com.aliyun.core.http.ProxyOptions;
import com.aliyun.httpcomponent.httpclient.ApacheAsyncHttpClientBuilder;
import com.aliyun.sdk.service.dysmsapi20170525.models.*;
import com.aliyun.sdk.service.dysmsapi20170525.*;
import com.google.gson.Gson;
import darabonba.core.RequestConfiguration;
import darabonba.core.client.ClientOverrideConfiguration;
import darabonba.core.utils.CommonUtil;
import darabonba.core.TeaPair;

//import javax.net.ssl.KeyManager;
//import javax.net.ssl.X509TrustManager;
import java.net.InetSocketAddress;
import java.time.Duration;
import java.util.*;
import java.util.concurrent.CompletableFuture;

import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.zzy.msm.service.MsmService;
import com.zzy.servicebase.exceptionhandler.GuliException;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Map;
import java.util.concurrent.ExecutionException;

/**
 * @Author KAZU
 * @Date 2023/1/19 10:41
 * @Version 1.0
 */
@SuppressWarnings({"all"})
@Service
public class MsmServiceImpl implements MsmService {
	
	/**
	 * 发送短信方法 java异步 aliyun_SDK
	 * */
	@Override
	public Boolean send(Map<String, Object> param, String phone) {
		StaticCredentialProvider provider = StaticCredentialProvider.create(Credential.builder()
				.accessKeyId("LTAI5t8oUzYC6VK7G87vjw7R")
				.accessKeySecret("DES0w61bPUBRQUofS74fbKxTjO4U2l")
				//.securityToken("<your-token>") // use STS token
				.build());
		
		AsyncClient client = AsyncClient.builder()
				.region("cn-shanghai") // Region ID
				//.httpClient(httpClient) // Use the configured HttpClient, otherwise use the default HttpClient (Apache HttpClient)
				.credentialsProvider(provider)
				//.serviceConfiguration(Configuration.create()) // Service-level configuration
				// Client-level configuration rewrite, can set Endpoint, Http request parameters, etc.
				.overrideConfiguration(
						ClientOverrideConfiguration.create()
								.setEndpointOverride("dysmsapi.aliyuncs.com")
						//.setConnectTimeout(Duration.ofSeconds(30))
				)
				.build();
		
		String code = JSONObject.toJSONString(param);
		
		SendSmsRequest sendSmsRequest = SendSmsRequest.builder()
				.signName("阿里云短信测试")
				.templateCode("SMS_154950909")
				.phoneNumbers(phone)
				.templateParam(code)
				// Request-level configuration rewrite, can set Http request parameters, etc.
				// .requestConfiguration(RequestConfiguration.create().setHttpHeaders(new HttpHeaders()))
				.build();
		
		CompletableFuture<SendSmsResponse> response = client.sendSms(sendSmsRequest);
		try {
			SendSmsResponse resp = response.get();
			System.out.println(new Gson().toJson(resp));
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			throw new GuliException(20001, "验证码发送失败");
		}
	}
	
	
	/**
	 * 发送短信方法
	 * */
//	@Override
//	public Boolean send(Map<String, Object> param, String phone) {
//		if (StringUtils.isEmpty(phone)) return false;
//
//		DefaultProfile profile = DefaultProfile.getProfile("default",
//				"LTAI5t8oUzYC6VK7G87vjw7R", "DES0w61bPUBRQUofS74fbKxTjO4U2l");
//		DefaultAcsClient client = new DefaultAcsClient(profile);
//
//		// 设置相关参数
//		CommonRequest request = new CommonRequest();
//		request.setMethod(MethodType.POST);
//		request.setDomain("dysmsapi.aliyuncs.com");
//		request.setVersion("2017-05-25");
//		request.setAction("SendSms");
//
//		// 设置发送相关参数
//		request.putQueryParameter("PhoneNumbers", phone);
//		request.putQueryParameter("SignName", "阿里云短信测试");
//		request.putQueryParameter("TemplateCode", "SMS_154950909");
//		request.putQueryParameter("templateParam", JSONObject.toJSONString(param));
//
//		// 发送
//		try {
//			CommonResponse response = client.getCommonResponse(request);
//			return response.getHttpResponse().isSuccess();
//		} catch (ClientException e) {
//			throw new GuliException(20001, "发送失败");
//		}
//	}
	
	
}












